
package com.cari.baju.yog;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.content.IntentSender;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.util.Log;
import android.widget.Toast;

import com.cari.baju.yog.entity.E_Toko;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.GoogleMap.OnInfoWindowClickListener;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapActivity extends FragmentActivity implements ConnectionCallbacks,
        OnConnectionFailedListener, OnInfoWindowClickListener {

    private GoogleMap map;

    private LocationClient locationClient;
    private Location location;

    private List<E_Toko> listToko;
    private List<E_Toko> tmp;

    private Bundle data;
    private double[] lat;
    private double[] lng;
    private int[] id;
    private String[] title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

        tmp = new ArrayList<E_Toko>();

        locationClient = new LocationClient(this, this, this);

        data = getIntent().getExtras();

        if (data != null) {
            lat = data.getDoubleArray("lat");
            lng = data.getDoubleArray("lng");
            id = data.getIntArray("id");
            title = data.getStringArray("title");
        }

    }

    /**
     * initialize map
     */
    private void setupMapIfNeeded() {
        if (map == null) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            SupportMapFragment supportMapFragment = (SupportMapFragment) fragmentManager
                    .findFragmentById(R.id.maps);
            map = supportMapFragment.getMap();

            if (map != null) {
                setupMap();
            }
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        locationClient.connect();
    }

    @Override
    protected void onStop() {
        super.onStop();
        locationClient.disconnect();
    }

    /**
     * Setup Map
     */
    private void setupMap() {
        map.setMyLocationEnabled(true);
        map.setOnInfoWindowClickListener(this);

        if (location != null) {
            map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(location.getLatitude(),
                    location.getLongitude()), 14));
        }

        // generate marker kedalam maps
        for (int i = 0; i < lat.length; i++) {
            map.addMarker(new MarkerOptions()
                    .position(new LatLng(lat[i], lng[i]))
                    .title(title[i])
                    );
        }
    }

    @Override
    public void onConnected(Bundle connectionHint) {
        // TODO Auto-generated method stub
        location = locationClient.getLastLocation();

        setupMapIfNeeded();
    }

    @Override
    public void onDisconnected() {
        // TODO Auto-generated method stub

    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        Toast.makeText(this, "Connection Failed", Toast.LENGTH_SHORT).show();
        if (result.hasResolution()) {
            Toast.makeText(this, "Result Has Resolution", Toast.LENGTH_LONG).show();
            try {
                result.startResolutionForResult(this, 1);
            } catch (IntentSender.SendIntentException e) {
                Log.d("error", "" + e.getMessage());
            }
        } else {
            Toast.makeText(this, "No Resolution", Toast.LENGTH_LONG).show();
        }

    }

    @Override
    public void onInfoWindowClick(Marker marker) {
        // TODO Auto-generated method stub

        String id = marker.getId();
        int index = Integer.parseInt(id.substring(1));

        Bundle data = new Bundle();
        data.putInt("id", this.id[index]);

        startActivity(new Intent(this, TokoActivity.class).putExtras(data));

    }
}
